<template>
  <el-dialog title="需求来源创建" :visible.sync="isOpen" v-if="isOpen" :destroy-on-close="true"
    :close-on-click-modal="false" width="500px" append-to-body @close="close">
    <el-form ref="form" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="需求来源" prop="department_name">
        <el-input v-model="formData.department_name" placeholder="请填写需求来源"></el-input>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio v-model="formData.status" :label="1">正常</el-radio>
        <el-radio v-model="formData.status" :label="2">停用</el-radio>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请填写备注"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  creatLookInfoModel,
  editLookInfoModel,
  departmentModelDetail,
} from "@/api/lookInfoModel";
export default {
  props: {
    id: {
      type: String,
      default: () => {},
    },
  },
  data() {
    return {
      isOpen: false,
      formData: {
        department_name: "",
        status: 1,
        remark: ""
      },
      deptOptions: [],
      selectDisabled: false,
      // 弹框
      rules: {
        department_name: [
          { required: true, message: "请填写需求的来源", trigger: "blur" },
        ],
      },
    };
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() {
    console.log(this.formData);
  },
  methods: {
    // 表单重置
    reset() {
      this.formData = {
        department_name: "",
        status: 1,
        remark: ""
      };
      this.resetForm("form");
    },
    // 取消按钮
    close() {
      this.reset();
      this.selectDisabled = false;
      this.isOpen = false;
      this.$emit("closeSuccess");
    },
    // 打开弹框
    open() {
      this.isOpen = true;
      // 判断创建还是编辑
      if (this.id) {
        this.selectDisabled = true;
        this.getLookInfoModelDetail(this.id);
      }
    },
    /** 提交按钮 */
    submitForm() {
      let that = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          // 区分编辑与创建
          if (that.id) {
            editLookInfoModel(that.formData).then((res) => {
              if (res.code == 200) {
                that.close();
                that.$emit("createSuccess");
              }
            });
          } else {
            creatLookInfoModel(that.formData).then((res) => {
              if (res.code == 200) {
                that.close();
                that.$emit("createSuccess");
              }
            });
          }
        }
      });
    },
    getLookInfoModelDetail(id) {
      let that = this;
      let datas = { id: id };
      departmentModelDetail(datas).then((res) => {
        if (res.code == 200) {
          that.detailsData = res.data;
          that.formData = that.detailsData;
          console.log(this.formData);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
/* @import url(); 引入css类 */
</style>